﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Objects;

namespace Fujitsu.JiraAnalysis.DAL
{
    public interface IResolutionStorage
    {
        List<ResolutionMapping> GetIssueResolutionMappingForProject(int projectId);
    }

    public class ResolutionStorage : IResolutionStorage
    {
        public List<ResolutionMapping> GetIssueResolutionMappingForProject(int projectId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return (from p in db.ResolutionMapping
                        where p.ProjectID == projectId
                        select p).ToList();
            }
        }

        public List<Resolution> GetResolutions()
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.Resolution.ToList();
            }
        }

        public void UpdateMapping(int projectId, int jiraResolutionId, int dbResolutionId, string jiraResolutionName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var resolutionMapping = (from resolution in db.ResolutionMapping
                                         where resolution.ProjectID == projectId
                                         && resolution.JiraResolutionID == jiraResolutionId
                                         && resolution.Name == jiraResolutionName
                                         select resolution).FirstOrDefault();

                var isToAddResolutionMapping = false;

                if (resolutionMapping != null)
                {
                    if (resolutionMapping.ResolutionID != dbResolutionId)
                    {
                        db.ResolutionMapping.DeleteObject(resolutionMapping);
                        isToAddResolutionMapping = true;
                    }
                }
                else
                {
                    isToAddResolutionMapping = true;
                }

                if (isToAddResolutionMapping)
                {
                    var resolutionNewMapping = new ResolutionMapping
                    {
                        ProjectID = projectId,
                        JiraResolutionID = jiraResolutionId,
                        ResolutionID = dbResolutionId,
                        Name = jiraResolutionName
                    };
                    try
                    {
                        db.ResolutionMapping.AddObject(resolutionNewMapping);
                        db.SaveChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        db.Refresh(RefreshMode.ClientWins, resolutionNewMapping);
                        db.SaveChanges();
                    }
                    catch (DataException)
                    {
                    }
                }
            }
        }

        public int GetResolutionMapping(int projectId, int jiraResolutionId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var resolutionMapping = db.ResolutionMapping.Where(r => r.ProjectID == projectId && r.JiraResolutionID == jiraResolutionId).FirstOrDefault();
                if (resolutionMapping != null)
                    return resolutionMapping.ResolutionID;
                return -1;
            }
        }

        public void UpdateResolutionName(int projectId, int jiraResolutionId, string resolutionName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var resolutionMap = db.ResolutionMapping.Where(
                        s => s.ProjectID == projectId && s.JiraResolutionID == jiraResolutionId).
                        FirstOrDefault();
                if (resolutionMap == null)
                    return;
                resolutionMap.Name = resolutionName;
                try
                {
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, resolutionMap);
                    db.SaveChanges();
                }
                catch (DataException)
                {
                }
            }
        }

        public List<ResolutionMapping> GetResolutionMappings(int projcetId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.ResolutionMapping.Where(i => i.ProjectID == projcetId).ToList();
                try
                {
                    return db.ResolutionMapping.Where(i => i.ProjectID == projcetId).ToList();
                }
                catch (EntityException ex)
                {
                    return new List<ResolutionMapping>();
                }
            }
        }

        public void DeleteResolutionMapping(int projectId, int jiraResolutionId, int resolutionId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var dbResolutionMapping = db.ResolutionMapping.FirstOrDefault(i => i.ProjectID == projectId && i.JiraResolutionID == jiraResolutionId && i.ResolutionID == resolutionId);
                if (dbResolutionMapping == null)
                    return;

                try
                {
                    db.ResolutionMapping.DeleteObject(dbResolutionMapping);
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, dbResolutionMapping);
                    db.SaveChanges();
                }
                catch (DataException ex)
                {
                    //EventLogger.CreateInstance(eventLog).LogWriteError(string.Concat("Can't delete issue from DB.", Environment.NewLine, ex.StackTrace));
                }
            }
        }

        public void DeleteResolutionMappingOfProject(int projcetId)
        {
            var statusMappings = GetResolutionMappings(projcetId);
            foreach (var priorityMapping in statusMappings)
            {
                DeleteResolutionMapping(priorityMapping.ProjectID, priorityMapping.JiraResolutionID, priorityMapping.ResolutionID);
            }
        }
    }
}
